package com.azt.provider.impl;

import com.azt.api.dto.PurchaseModel;
import com.azt.api.pojo.EnqEnquiryItem;
import com.azt.api.pojo.ProRepository;
import com.azt.api.pojo.ProRepositoryTitles;
import com.azt.api.service.EnqEnquiryAnalysisService;
import com.azt.api.service.ProRepositoryService;
import com.azt.api.service.ProRepositoryTitleService;
import com.azt.utils.MyStringUtils;
import com.azt.utils.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 询价分析
 *
 * @author 张栋 2016年8月10日上午11:45:51
 */
@Service
@Transactional
public class EnqEnquiryAnalysisServiceImpl implements EnqEnquiryAnalysisService {

    @Autowired
    private ProRepositoryService repositoryService;

    @Autowired
    private ProRepositoryTitleService repositoryTitleService;

    @Autowired
    private ParseExcel parseExcel;

    @Override
    public PurchaseModel tryPurchase(String path, Integer adminid, Integer type) throws Exception {
        return parseExcel.tryPurchase(path, adminid, type);
    }

    @Override
    public void saveOrUpdateRepositoryTitles(String[] typenames, String[] oldtitles, int type) {
        if (typenames == null || oldtitles == null || typenames.length != oldtitles.length)
            return;

        for (int i = 0; i < typenames.length; i++) {
            String typename = typenames[i];
            String oldtitle = oldtitles[i];
            String content = MyStringUtils.replaceBlank(oldtitle);
            String secret = PasswordUtil.MD5_16(content);
            ProRepositoryTitles title = repositoryTitleService.getRepositoryTitleBymd5(secret, type);
            if (title == null) {
                title = new ProRepositoryTitles();
                title.setSecret(secret);
                title.setTitle(content);
            }
            title.setPid(Integer.parseInt(typename));
            title.setType(type);
            repositoryTitleService.saveOrupdateRepositoryTitle(title);
        }
    }

    @Override
    public void saveOrUpdateRepository(List<EnqEnquiryItem> item) {
        // 保存未知系统到数据库
        for (int i = 0; i < item.size(); i++) {
            EnqEnquiryItem enquiry = item.get(i);
            Integer needrepository = enquiry.getNeedrepository();
            if (needrepository!=null && needrepository == 1) {// 需要保存到库
                String content = MyStringUtils.replaceBlank(enquiry.getProductName());
                String secret = PasswordUtil.MD5_16(content);
                List<ProRepository> repositorys = repositoryService.getRepositoryBymd5(secret);
                boolean needsave = true;
                if (repositorys == null || repositorys.isEmpty()) {
                    // 不存在
                    needsave = true;
                } else {
                    //看匹配的结果里是不是和当前有相同的,有相同的就不需要保存的
                    for (int j = 0; j < repositorys.size(); j++) {
                        ProRepository proRepository = repositorys.get(j);
                        Integer fcateid = enquiry.getCategoryPId();
                        // 如果有完成相同的,则不用保存
                        if (proRepository.getPid().equals(fcateid)) {
                            // 不需要保存,直接退出
                            needsave = false;
                            break;
                        }

                    }
                }

                if (needsave) {
                    //保存到匹配仓库去
                    ProRepository repository = new ProRepository();
                    repository.setTitle(content);
                    repository.setSecret(secret);
                    repository.setPid(enquiry.getCategoryPId());
                    repositoryService.saveOrupdateRepository(repository);
                }

            }
        }
    }

    @Override
    public List<PurchaseModel> tryPurchaseMulity(String path, Integer adminid, Integer type) throws Exception {
        return parseExcel.tryPurchaseMulity(path, adminid, type);
    }

}
